package com.example.administrator.exkotlin.multimedia.notify

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.app.NotificationCompat
import com.example.administrator.exkotlin.R
import kotlinx.android.synthetic.main.activity_notify.*

/**
 * 通知的使用
 * 通知可以在activity，service，BroadcastReceiver中创建
 * 8.0之后引入渠道通知，可以定义不同的通知级别
 * */
class NotifyActivity : AppCompatActivity() {
    private val channel_id_1 = "test_id_1_111"
    private val channel_name_1 = "默认通知"
    private val channel_id_2 = "test_id_hight"
    private val channel_name_2 = "重要通知"
    private lateinit var manager: NotificationManager
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_notify)
        //创建NotifyMannager
        creatManneager()


        btn_create_notify.setOnClickListener {
            val createNotify = createNotify(channel_id_1)
            val createNotify2 = createNotify(channel_id_2)
            manager.notify(1, createNotify)
            manager.notify(2, createNotify2)
        }
        btn_cancel_notify.setOnClickListener {
//            manager.cancel(1)
            manager.cancel(2)
        }

    }


    private fun creatManneager() {
        manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        //如果大于8.0,则可以创建通知渠道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //channel_id 通知id，在创建之后就不能被修改了，会一直存在在系统的应用通知设置中
            val chanel = NotificationChannel(channel_id_1, channel_name_1, NotificationManager.IMPORTANCE_DEFAULT)
            manager.createNotificationChannel(chanel)
            val chanel2 = NotificationChannel(channel_id_2, channel_name_2, NotificationManager.IMPORTANCE_HIGH)
            manager.createNotificationChannel(chanel2)

        }
    }

    private fun createNotify(channelId: String): Notification {
        //通知点击意图
        val pendingIntent = PendingIntent.getActivity(this, 0, Intent(this, NotifyActivity::class.java), 0)

        //构建
        val notify = NotificationCompat.Builder(this, channelId)
                .setContentTitle("---标题---")
                .setContentText("----内容------")
                .setStyle(NotificationCompat.BigTextStyle().bigText("通知（Notification）是应用为用户显示重要信息提示的一种工具。系统为开发者提供了不同种类的通知样式模板可以使用，开发者也可以根据自己需要自定义通知样式"))
//                .setStyle(NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(resources, R.drawable.test)))
                .setSmallIcon(R.drawable.notify_icon)
                .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.test))
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .build()
        return notify
    }

}
